<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img :src="logoPath" />
      </div>

      <!-- 登录表单区 -->
      <el-form ref="loginFormRef" size="large" label-width="0px" :rules="loginFormRules" :model="loginForm"
        class="login_form">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="用户名/邮箱/手机号" v-model="loginForm.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="密码" show-password v-model="loginForm.password" />
        </el-form-item>
        <el-form-item prop="captcha">
          <el-row :gutter="2" style="width: 100%;">
            <el-col :span="18">
              <el-input style="width: 75%;" placeholder="验证码" v-model="loginForm.captcha">
                <template #prefix>
                  <i class="fa fa-get-pocket"></i>
                </template>
              </el-input>
            </el-col>
            <el-col :span="6">
              <el-image :src="captchaPath" @click="changeCaptcha">
                <template #placeholder>
                  <div style="background-color: #F5F7FA; width: 100px; height: 40px; text-align: center; color: #C0C4CC;">
                    加载中<span class="dot">...</span>
                  </div>
                </template>
                <template #error>
                  <div style="background-color: #F5F7FA; width: 100px; height: 40px; text-align: center; color: #C0C4CC;">
                    <span @click="changeCaptcha">加载失败</span>
                  </div>
                </template>
              </el-image>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="appLogin(loginFormRef)" style="width:100%;">登&nbsp;&nbsp;&nbsp;&nbsp;录
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="ICP">
      <span>@2019-2099&nbsp;&nbsp;立群工作室&nbsp;&nbsp;版权所有</span>&nbsp;&nbsp;<a href="https://beian.miit.gov.cn/"
        target="_blank">皖ICP备19020518号-1</a>
    </div>
  </div>
</template>

<script lang="ts">
export default { name: 'AutumnLogin' }
</script>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { login } from 'src/api/home'
import { setUser } from 'src/util/auth'
import logo from 'src/assets/logo.png'
import { $genUUID } from 'src/hooks/useCommon'

const logoPath = logo

const loginFormRef = ref<FormInstance>()

const loginForm = reactive({
  username: '',
  password: '',
  captchaToken: '',
  captcha: ''
})

const loginFormRules = {
  username: [
    { required: true, message: '请输入用户名/邮箱/手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

const captchaPath = ref('#')
const changeCaptcha = function () {
  const uuid = $genUUID()
  loginForm.captchaToken = uuid
  captchaPath.value = import.meta.env.VITE_APP_SERVICE_URL + `/captcha?token=${uuid}`
}
changeCaptcha()

const router = useRouter()

const appLogin = function (formEl: FormInstance | undefined) {
  if (!formEl) return

  formEl.validate((valid: boolean) => {
    if (!valid) return

    login(loginForm).then(({ data }) => {
      setUser(data)
      router.push('/dashboard')
    }).catch(() => {
      changeCaptcha()
    })
  })
}

onMounted(() => {
  document.onkeydown = (e) => {
    if (e.key === 'Enter') {
      appLogin(loginFormRef.value)
    }
  }
})

</script>

<style lang="scss" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.el-image {
  position: relative;
  border: 0 solid #eee;
  height: 38px;
  float: right;
}

.ICP {
  position: absolute;
  bottom: 0px;
  left: 40%;
  text-align: center;
  font-size: 12px;

  a {
    text-decoration: none;
    color: #000;

    &:hover {
      color: #ffcc00;
    }
  }
}
</style>
